<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractEditAction.class.php';

/**
 * Mileage_Admin_MileEditAction
**/
class Mileage_Admin_MileEditAction extends Mileage_AbstractEditAction
{
	/**
	 * _getId
	 * 
	 * @param   void
	 * 
	 * @return  int
	**/
	protected function _getId()
	{
		return $this->mRoot->mContext->mRequest->getRequest('mile_id');
	}

	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'mile');
		return $handler;
	}

	/**
	 * _setupActionForm
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	protected function _setupActionForm()
	{
		// $this->mActionForm =& new Mileage_Admin_MileEditForm();
		$this->mActionForm =& $this->mAsset->getObject('form', 'mile', TRUE, 'edit');
		$this->mActionForm->prepare();
	}

	/**
	 * executeViewInput
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewInput(&$render)
	{
		$cattree = Mileage_Utils::getXcat($this->mAsset->mDirname);
		if (!empty($cattree)) {
			$group = $this->mObject->get('group');
			$group = trim($group, '[]');
			$group_arr = explode('][', $group);
			foreach ($cattree as $k => $v) {
				if (in_array($v['cat_id'], $group_arr)) {
					$cattree[$k]['default'] = $v['cat_id'];
				}
			}
		}
	
		$render->setTemplateName('mile_edit.html');
		$render->setAttribute('actionForm', $this->mActionForm);
		$render->setAttribute('object', $this->mObject);
		$render->setAttribute('cattree', $cattree);
		$render->setAttribute('interval_selector', $this->_makeIntervalSelector($this->mObject->get('interval')));
	}

	/**
	 * _makeIntervalSelector
	 * 
	 * @param   intger  $interval
	 * 
	 * @return  string
	**/
	private function _makeIntervalSelector($interval)
	{
		$selected0 = $selected1 = $selected2 = $selected3 = $selected4 = $selected99 = '';
		$selected = 'selected' . $interval;
		$$selected = ' selected="SELECTED" ';
		$sel  = '<select name="interval">';
		$sel .= '<option value="0"' . $selected0 . '>' . _MD_MILEAGE_INTERVAL_0 . '</option>';
		$sel .= '<option value="1"' . $selected1 . '>' . _MD_MILEAGE_INTERVAL_1 . '</option>';
		$sel .= '<option value="2"' . $selected2 . '>' . _MD_MILEAGE_INTERVAL_2 . '</option>';
		$sel .= '<option value="3"' . $selected3 . '>' . _MD_MILEAGE_INTERVAL_3 . '</option>';
		$sel .= '<option value="4"' . $selected4 . '>' . _MD_MILEAGE_INTERVAL_4 . '</option>';
		$sel .= '<option value="99"' . $selected99 . '>' . _MD_MILEAGE_INTERVAL_99 . '</option>';
		$sel .= '</select>';
		return $sel;
	}

	/**
	* executeViewSuccess
	* 
	* @param   XCube_RenderTarget  &$render
	* 
	* @return  void
	**/
	public function executeViewSuccess(/*** XCube_RenderTarget ***/ &$render)
	{
		$this->mRoot->mController->executeForward('./index.php?action=MileList');
	}

	/**
	 * executeViewError
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewError(/*** XCube_RenderTarget ***/ &$render)
	{
		$this->mRoot->mController->executeRedirect('./index.php?action=MileList', 1, _MD_MILEAGE_ERROR_DBUPDATE_FAILED);
	}

	/**
	 * executeViewCancel
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewCancel(/*** XCube_RenderTarget ***/ &$render)
	{
		$this->mRoot->mController->executeForward('./index.php?action=MileList');
	}

}
